﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ZHDJ.Data.Models.Org;
using ZHDJ.WebPage.Picker;
using FineUI;
using ZHDJ.Core.Caching;
using ZHDJ.WebPage;

namespace ZHDJ.Web.App.Share.Picker
{
    public partial class BranchSinglePicker : PickerPageBase
    {
        /// <summary>
        /// 
        /// </summary>
        public string BranchID
        {
            get { return Request.QueryString["BranchID"] ?? ""; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
                LoadData();
            }
        }

        private void LoadData()
        {
            var selected = SelectValue;
            if (selected.Count == 1)
            {
                var node = TreeBranch.FindNode(selected[0]);
                if (node != null)
                {
                     node.Checked = true;
                    //node.CommandArgument为父节点NodeID
                    while (node!=null&&!string.IsNullOrEmpty(node.CommandArgument))
                    {
                        node = TreeBranch.FindNode(node.CommandArgument);
                        if (node != null)
                        {
                            node.Expanded = true;
                        }
                    }
                }
            }
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
           
        }

       
        private void BindTree()
        {

            TreeControlBusiness tree = new TreeControlBusiness("BranchID", "ParentBranchID", "BranchName", true, CacheKeys.Cache_Branch_Tree_Picker);
            tree.EnableCheckEvent = true;
            tree.IsAllCheckBox = true;
            tree.BindTreeBranch(TreeBranch);
            if (!string.IsNullOrEmpty(BranchID))
            {
                var n = TreeBranch.FindNode(BranchID);
               
                TreeBranch.Nodes.Clear();
                if (n != null)
                    TreeBranch.Nodes.Add(n);
            }
            if (TreeBranch.Nodes.Count > 0)
            {
                //展开第一层
                TreeBranch.Nodes[0].Expanded = true;
            }
            //取消所有选中（避免缓存）
            TreeNode[] nodes = TreeBranch.GetCheckedNodes();
            if (nodes.Length > 0)
            {
                //取消选中
                foreach (TreeNode node in nodes)
                {
                    node.Checked = false;
                }
            }
        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            TreeNode[] nodes = TreeBranch.GetCheckedNodes();
            if (nodes.Count() != 1)
            {
                Alert.Show("请选择一个组织！", String.Empty);
                return;
            }
            base.ReturnValue(nodes[0].NodeID, nodes[0].Text);
        }

    
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            ttbSearchMessage.ShowTrigger1 = true;
          // BindGrid();
        }

        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            ttbSearchMessage.Text = String.Empty;
            ttbSearchMessage.ShowTrigger1 = false;
            //BindGrid();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeBranch_NodeCheck(object sender, TreeCheckEventArgs e)
        {
            //获取选中集合
            TreeNode[] nodes = TreeBranch.GetCheckedNodes();
            if (nodes.Length > 0)
            {
                //取消选中
                foreach (TreeNode node in nodes)
                {
                    if (node.NodeID != e.NodeID)
                        node.Checked = false;
                }
            }
        }
    }
}